import axios from 'axios';
import store from './store';
import router from './router';
/* eslint-disable */
const axiosInstance = axios.create({
	// baseURL: 'http://suzuki-dev-finance.haimaiche.net:8999',
	// baseURL: 'http://172.21.3.98:8080', //172.21.1.190
	// baseURL: 'http://localhost:8080',
	baseURL: '',
	headers: {
		// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
		// 'Content-Type': 'application/json; charset=UTF-8'
	},
	// withCredentials: true, //是否是跨域请求
	timeout: 100000,
});

axiosInstance.interceptors.request.use(function(config) { // 这里的config包含每次请求的内容
	if (store.state.token) {
		config.headers['x-auth-token'] = `${store.state.token}`;
	}
	return config;
}, function(err) {
	return Promise.reject(err);
});


axiosInstance.interceptors.response.use(
	response => {
		switch (response.data.code) {
			case 203:
				// 返回 203 清除token等登录相关信息并跳转到登录页面
				router.push({
					path: '/login',
				});
				sessionStorage.removeItem('access-user');
				sessionStorage.removeItem('token');
				sessionStorage.removeItem('power');
				sessionStorage.removeItem('side');
				this.$store.state.token = '';
				this.$store.state.power = '';
				this.$message({
					message: '登录超时，请重新登录！',
					type: 'warning'
				});
		}
		return response;
	},
	error => {
		if (error.response) {

		}
		return Promise.reject(error.response.data) // 返回接口返回的错误信息
	});
export default axiosInstance;
